Back to course home
0% completed
Vote For New Content
Amazon Hard Problems
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Count the Number of Good Subarrays (medium)
Problem Statement
Given an array of integers nums, and an integer k, find the count of "good" subarrays within nums.
A subarray is considered "good" if it contains at least k pairs of elements (i, j) where i < j and nums[i] == nums[j].
A subarray is a contiguous sequence of elements in the original array.
Examples
-
Example 1:
- Input:
nums = [2, 2, 2, 3, 3], k = 3 - Expected Output:
3 - Justification: There are 3 good subarrays that meet the criteria:
[2, 2, 2, 3],[2, 2, 2, 3, 3], and[2, 2, 2]. Each of these subarrays has at least 3 pairs of equal elements.
- Input:
-
Example 2:
- Input:
nums = [4, 4, 4, 4], k = 6 - Expected Output:
1 - Justification: The only good subarray that meets the criteria is the entire array itself
[4, 4, 4, 4], which contains 6 pairs of equal elements.
- Input:
-
Example 3:
- Input:
nums = [1, 2, 3, 1, 2], k = 2 - Expected Output:
2 - Justification: There is only 1 good subarray, which is
[1, 2, 3, 1, 2]. It contains at least 2 pairs of equal elements.
- Input:
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Largest Submatrix With Rearrangements
Solution: Count the Number of Good Subarrays
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself